// LGUIFileList.cpp: implementation of the LGUIFileList class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LGUIFileList.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LGUIFileList::LGUIFileList(int id, int _x, int _y, int _w, int _h, String _path, bool _allowBrowsing, bool _allowBrowsingOutside) : LGUIListBox(id,_x,_y,_w,_h)
{
  path=_path;
  basePath=path;
  allowBrowsing=_allowBrowsing;
  allowBrowsingOutside=_allowBrowsingOutside;
  refresh();
}

LGUIFileList::~LGUIFileList()
{

}


void LGUIFileList::onClick(int n)
{
  String text=items[n].text;
  if (text==String(TEXT("((UP))")))
  {
    path=path.subString(0,path.findLast(TEXT('\\'),1)+1);
    refresh();
  }
  else if (text[0]=='(')
  {
    path=path + text.subString(1,text.length()-2) + TEXT('\\'); 
    refresh();
  }
}


//Let's build a list of files in the current directory
void LGUIFileList::refresh()
{
  clear();
#ifdef WINCE
  enum {rootLen=4};
#else
  enum {rootLen=6};
#endif
  
  //should we include an option to go upwards in the directory structure?
  if (allowBrowsing && path.length()>rootLen)
  {
    bool isTopLevel = path.length()==basePath.length();
    if (allowBrowsingOutside || !isTopLevel)
      addItem(TEXT("((UP))"));
  }
  
  //Time to fill the list
  WIN32_FIND_DATA find;
  HANDLE hSearch = FindFirstFile(path+TEXT("*.*"),&find);

  //If the directory wasn't empty...
  if (hSearch != INVALID_HANDLE_VALUE)
  {
    //start looking!
    bool isFilesLeft=true;
    while (isFilesLeft) {
      String fname=find.cFileName;

      bool skip=false;
      
      //Only needed on non-CE windows. Lets skip these special directory names.
      if (fname==String(TEXT(".."))) 
        skip=true;
      if (fname==String(TEXT("."))) 
        skip=true;
        
      if (!allowBrowsing) //skip directories if not allowed to browse
      {
        if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
          skip=true;
      }
      else
      {
        //This is a cheap hack: identify directories by enclosing them in parenthesis. 
        //Actually filenames MAY contain parenthesis so this is NOT a good solution
        if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
          fname=TEXT("(") + fname + TEXT(")");
      }

      if (!skip)
        addItem(fname); //add it!

      isFilesLeft = FindNextFile(hSearch,&find) ? true : false; //get the next one
    }
  }
  sort(); //to make it easy for the user
}


String LGUIFileList::getFullFilename()
{
  if (getSelectedItem()>=0 && getSelectedItem()<getNumItems())
    return path + items[getSelectedItem()].text;
  else
    return String(TEXT(""));
}


String LGUIFileList::getRelativeFilename()
{
  String temp=getFullFilename();
  if (temp.find(basePath,0)==0)
  {
    return temp.subString(basePath.length(),1000); //1000 means the rest of the path :) it will be clipped by subStr();
  }
  else
  {
    //We have a problem
    MessageBox(0,TEXT("Problem in LGUIFileList::getRelativeFileName()!! getFullFileName() returned a bad path"),TEXT("Error"),0);
    return String(TEXT(""));
  }
}